// 2013-7-29下午06:21:38  net.betou.trade.main.manager.impl.NoticeMngImpl.java

package net.betou.trade.main.manager.impl;

import java.util.List;

import net.betou.common.hibernate3.Updater;
import net.betou.common.page.Pagination;
import net.betou.core.service.PostDataListener;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Notice;
import net.betou.trade.entity.main.User;
import net.betou.trade.main.dao.NoticeDao;
import net.betou.trade.main.manager.NoticeMng;
import net.betou.trade.main.manager.NoticeTypeMng;
import net.betou.trade.main.manager.UserMng;
import net.betou.trade.role.setting.NoticeFinderConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author 叶装装
 */
@Service
@Transactional
public class NoticeMngImpl implements NoticeMng{

	public Notice deleteById( Integer id ) {
		Notice bean = dao.deleteById( id );
		
		return bean;
	}

	public Notice [ ] deleteByIds( Integer [ ] ids ) {
		Notice [ ] beans = new Notice[ ids.length ];
		
		for( int i = 0; i < ids.length; i++ ){
			beans[ i ] = deleteById( ids[ i ] );
		}
		return beans;
	}
	
	@Transactional( readOnly = true )
	public Notice getLastNotice( ){
		return dao.getLastNotice( );
	}
	
	@Transactional( readOnly = true )
	public Notice findById( Integer id ) {
		return dao.findById( id );
	}
	
	@Transactional( readOnly = true )
	public Notice findMyNotice( Integer id, Integer selfId, Boolean system ) {
		Notice notice;
		if( system ){
			notice = dao.findById( id );
		}else{
			notice = dao.findMyNotice( id, selfId );
		}
		
		return notice;
	}
	
	@Transactional( readOnly = true )
	public Pagination getPage( AgentAndAdmins admin, String searchProperty, String searchValue, String orderProperty, String orderDirection, String filterStatus, int pageNo, int pageSize ) {
		Boolean system = admin.getSystem( );
		Pagination pagination;
		
		if( system ){
			pagination = dao.getPage( null, searchProperty, searchValue, orderProperty, orderDirection, filterStatus, pageNo, pageSize );
		}else{
			// 要查询自己的和系统的公告
			
			NoticeFinderConfig config = new NoticeFinderConfig( admin );
			
			if( config.isSelf( ) ){
				pagination = dao.getPage( admin.getId( ), searchProperty, searchValue, orderProperty, orderDirection, filterStatus, pageNo, pageSize );
			}else{
				
				pagination = dao.getPageByCriteria( searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize, config.getDeepSQL( ) );
			}
		}
		
		return pagination;
	}
	
	@Transactional( readOnly = true )
	public Pagination getPage(  User user,  int pageNo, int pageSize ){
		
		List< Integer > idList = userMng.getSearchId( user );
		if( idList.isEmpty( ) ){
			idList.add( user.getAdmin( ).getId( ) );
		}
		
		return dao.getPage(  user.getGroup( ).getId( ), idList, pageNo, pageSize );
	}

	public Notice save( Notice bean, AgentAndAdmins admin, Integer [ ] mxGroups, Integer [ ] smsGroups ) {
		bean.init( );
		
		if( admin.getSystem( ) ){
			bean.setNoticeType( mng.findBySystem( ) );	
		}else{
			bean.setNoticeType( mng.findByAgent( ) );
		}
		
		if( mxGroups != null ){
			bean.setOrientedGroup( splitUserGroup( mxGroups ) );
		}
		
		if( smsGroups != null ){
			bean.setSmsSendGroup( splitUserGroup( smsGroups ) );
		}
		
		bean.setAdmin( admin );
		
		dao.save( bean );
		
		afterSave( bean );
		
		return bean;
	}

	public Notice update( Notice bean, Integer [ ] mxGroups, Integer [ ] smsGroups ) {
		
		if( mxGroups != null ){
			bean.setOrientedGroup( splitUserGroup( mxGroups ) );
		}
		
		if( smsGroups != null ){
			bean.setSmsSendGroup( splitUserGroup( smsGroups ) );
		}
		
		Updater< Notice > updater = new Updater< Notice >( bean, Updater.UpdateMode.MIN ); 
		
		updater.include( "title" ); // 使用最小模式
		updater.include( "content" );
		updater.include( "orientedGroup" );
		updater.include( "smsSendGroup" );
		updater.include( "smsContent" );
		
		Notice newBean = dao.updateByUpdater( updater );
		
		newBean.setImmediately( bean.getImmediately( ) ); 
		
		afterUpdate( newBean );
		
		return bean;
	}
	
	private void afterUpdate( Notice bean ){
		for( PostDataListener< Notice > p : listenerList ){
			p.afterChange( bean, null );
		}
	}
	
	private void afterSave( Notice notice ){
		for( PostDataListener< Notice > p : listenerList ){
			p.afterSave( notice );
		}
	}
	
	private String splitUserGroup( Integer [ ] userGroup ){
		String group = "";
		if( userGroup != null ){
			for( int i = 0; i < userGroup.length; i++ ){
				group += userGroup[ i ];
				if( i < userGroup.length - 1 ){
					group += ",";
				}
			}
		}
		
		return group;
	}
	
	private List< PostDataListener< Notice > > listenerList;
	
	public void setListenerList( List< PostDataListener < Notice >> listenerList ) {
		this.listenerList = listenerList;
	}
	
	
	@Autowired
	private NoticeDao dao;
	
	@Autowired
	private NoticeTypeMng mng;
	
	@Autowired
	private UserMng userMng;

}
